//
//  PostDeailView.swift
//  skincare
//
//  Created by whu125 on 2023/4/24.
//

import SwiftUI
import ACarousel

struct PostDetailView: View {
    func returnImg(urlString:String)-> Image{
        
//        if let url_s = URL(string: url) {
//            print(url_s)
//            if let data = try? Data(contentsOf: url_s) {
//                print(data)
//                if let image = UIImage(data: data) {
//                    return Image(uiImage: image)
//                }
//            }
//        }
//        return Image(systemName: "heart.fill")
        guard let url = URL(string: urlString),
              let data = try? Data(contentsOf: url),
              let uiImage = UIImage(data: data)
        else {
            print("Failed to load image from URL: \(urlString)")
            return Image(systemName: "photo")
        }

       // print("Loaded image from URL: \(urlString)")
        return Image(uiImage: uiImage)
    }
    let roles: [String]
    init(post: Post) {
           self.post = post
           roles = [post.image]
       }
    
    
    let post: Post
    
    
      var body: some View {
          ScrollView {
              VStack {
                  HStack {
                      Image("icon")
                          .resizable()
                          .aspectRatio(contentMode: .fit)
                          .frame(maxWidth: 50)
                      Rectangle()
                          .fill(Color(red: 42/255, green: 213/255, blue: 210/255, opacity: 1))
                          .frame(width: 3, height: 35, alignment: .leading)
                      VStack(alignment: .leading) {
                          Text("Hello, World!")
                              .font(.title2)
                          Text("Skincare审稿委员会审核通过")
                              .font(.caption)
                              .foregroundColor(Color(red: 0.153, green: 0.831, blue: 0.827))
                      }
                      ZStack {
                          Rectangle()
                              .fill(Color(red: 0.153, green: 0.831, blue: 0.827))
                              .cornerRadius(20)
                              .frame(width: 120, height: 40)

                          Text("2023-05-10")
                              .foregroundColor(Color.white)
                              .font(.body)
                      }
                  }
                  //轮播图
                  ACarousel(roles, id: \.self) { url in
                      returnImg(urlString: url)
                          .resizable()
                          .scaledToFill()
                          .frame(height: 300)
                          .cornerRadius(30)
                  }
                  .padding(.top, 10.0)
                  .frame(height: 300)
              }

             // GeometryReader { geometry in
                  VStack(alignment:.leading) {
                          Text(post.body)
                              .padding(.leading, 20.0)
                              .frame(maxWidth: .infinity, alignment: .leading)
                      }
//                  .frame(width: geometry.size.width)
             // }
          }
      }
}

//struct PostDetailView_Previews: PreviewProvider {
//    static var previews: some View {
//        PostDetailView()
//    }
//}

//临时画面
//轮播图
struct Item: Identifiable {
    let id = UUID()
    let image: Image
}


//底部栏

//struct Previews_PostDetailView_Previews: PreviewProvider {
//
//    let items: [Item] = roles.map { Item(image: Image($0)) }
//
//    static var previews: some View {
//
//}

//struct PostDetailView_Previews: PreviewProvider {
//    
//  static let post = Post(image: "Sample Post", title: "This is a sample post.")
//    
//  static var previews: some View {
//      PostDetailView(post: post)
//  }
//}

